/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.com.itg;

import cn.com.itg.function.process.PaperKafkaSchema;
import cn.com.itg.pojo.kafka.KafkaConsumerRecord;
import org.apache.flink.streaming.api.TimeCharacteristic;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;

import java.util.Properties;

/**
 * Skeleton for a Flink Streaming Job.
 *
 * <p>For a tutorial how to write a Flink streaming application, check the
 * tutorials and examples on the <a href="https://flink.apache.org/docs/stable/">Flink Website</a>.
 *
 * <p>To package your application into a JAR file for execution, run
 * 'mvn clean package' on the command line.
 *
 * <p>If you change the name of the main class (with the public static void main(String[] args))
 * method, change the respective entry in the POM.xml file (simply search for 'mainClass').
 */
public class StreamingJob {

	public static void main(String[] args) throws Exception {
		StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
		env.getConfig().setAutoWatermarkInterval(200);
		env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);
        /*
            设置kafka消费配置
         */
		Properties kafkaProperties = getKafkaProperties("172.20.1.59:9092", "itg_paper_stream");

		//从kafka拉取MATDOC
		FlinkKafkaConsumer<KafkaConsumerRecord> kafkaConsumer =
				new FlinkKafkaConsumer<KafkaConsumerRecord>(
						"ITGDTP-SAP-MATDOC-TOPIC",
						new PaperKafkaSchema(),
						kafkaProperties
				);
		DataStreamSource<KafkaConsumerRecord> source = env.addSource(kafkaConsumer).setParallelism(1);
		source.name("source");
		source.print("kafkasource");
		env.execute();

	}
	private static Properties getKafkaProperties(String bootstrap, String group) {
		//创建matdoc数据源
		Properties properties = new Properties();
		//封装kafka的连接地址
		properties.setProperty("bootstrap.servers", bootstrap);
		//指定消费者id
		properties.setProperty("group.id", group);
		//设置动态监测分区或者主题的变化
		properties.setProperty("flink.partition-discovery.interval-millis", "30000");

		//消息key的反序列化方式
		properties.setProperty("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
		//消息value的反序列化方式
		properties.setProperty("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
		//消息拉取策略-
		properties.setProperty("auto.offset.reset", "earliest");
		return properties;
	}
}
